package com.ljsw.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljsw.application.App;
import com.ljsw.fragment.BaseFragment.OnHeadlineSelectedListener;
import com.ljsw.fragment.GyFragment;
import com.ljsw.fragment.KssmFragment;
import com.ljsw.fragment.MyRenwuFragment;
import com.ljsw.fragment.MyRenwuFragment.MyRwListener;
import com.ljsw.fragment.RwwcFragment;
import com.ljsw.fragment.RwwcFragment.OnLoadZhuangXiangView;
import com.ljsw.fragment.StartFwFragment;

/**
 * 上门服务主页面 作者: 石锚 版本: 1.0 日期: 2015-10-16
 */
public class SmfwMainActivity extends FragmentActivity implements
        OnClickListener, MyRwListener, OnLoadZhuangXiangView,
        OnHeadlineSelectedListener {
  //fragment 管理
    private FragmentManager fm;
    private FragmentTransaction transaction;
    // 开启一个Fragment事务
    private RelativeLayout renwu,// 我的任务
            wancheng,// 任务完成
            guanyu;// 关于
    private TextView renwuNum, zhuangxiangNum;// 我的任务条数
    private TextView back;// 返回键
    private MyRenwuFragment myfragment;// 我的任务fragment
    private RwwcFragment rwccfragment;// 任务完成fragment
    private GyFragment gyfragment;// 关于
    private LinearLayout parent;// 父线性布局
    private App app; // 全局变量

    private String cid;// 接受返回的车牌号

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_mian_smfw);
        app = (App) getApplication();
        // 启动activity时不自动弹出软键盘
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        fm = getSupportFragmentManager();

        transaction = fm.beginTransaction();
        /*
         * if(myfragment == null){ // 如果Fragment为空，则创建一个并添加到界面上 myfragment = new
		 * MyRenwuFragment(); }
		 */
        /*
         * if(rwccfragment == null){ // 如果Fragment为空，则创建一个并添加到界面上 rwccfragment =
		 * new RwwcFragment(); } if(gyfragment == null){ //
		 * 如果Fragment为空，则创建一个并添加到界面上 gyfragment = new GyFragment(); }
		 */
        if (arg0 == null) {
            myfragment = new MyRenwuFragment();
            transaction.replace(R.id.main_frame, myfragment).commit();
        }
        if (arg0 != null) {
            String FRAGMENTS_TAG = "android:support:fragments";
            arg0.remove(FRAGMENTS_TAG);
        }
        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    /**
     * 初始化控件
     */
    public void initView() {
        parent = (LinearLayout) this.findViewById(R.id.parent);
        back = (TextView) this.findViewById(R.id.back); // 返回按钮
        renwu = (RelativeLayout) this.findViewById(R.id.renwu);// 我的任务
        wancheng = (RelativeLayout) this.findViewById(R.id.wancheng);// 完成任务
        guanyu = (RelativeLayout) this.findViewById(R.id.guanyu);// 关于
        renwuNum = (TextView) this.findViewById(R.id.renwuNum);// 我的任务信息条目
        zhuangxiangNum = (TextView) this.findViewById(R.id.zhuangxiangNum);// 装箱任务条数目录回库交接
        /** 绑定监听事件 */
        parent.setOnClickListener(this);
        renwu.setOnClickListener(this);
        wancheng.setOnClickListener(this);
        guanyu.setOnClickListener(this);
        back.setOnClickListener(this);

        /** 加载默认页面 */
        // transaction.addToBackStack(null);
        app.back = "back";// 返回键的标识
        renwu.setBackgroundResource(R.drawable.segmentedbtn1_u14_selected);
    }

    /**
     * 点击切换fragment
     */
    public void onClick(View arg0) {
        FragmentTransaction transaction = fm.beginTransaction();
//        for (int i = 0; i < fm.getBackStackEntryCount(); i++) { 原版放在这复用后因由误触的原因放到具体的case中
//            Log.d("smffwmanactivity", ""+fm.getBackStackEntryCount());
//            fm.popBackStack();
//        }

        switch (arg0.getId()) {
            case R.id.renwu:// 我的任务
                for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
                    Log.d("smffwmanactivity", "fragment的数量" + fm.getBackStackEntryCount());
                    fm.popBackStack();
                }
                // transaction.addToBackStack(null);
                transaction.replace(R.id.main_frame,
                        myfragment = new MyRenwuFragment()).commit();
                renwu.setBackgroundResource(R.drawable.segmentedbtn1_u14_selected);
//                wancheng.setBackgroundResource(R.drawable.segmentedbtn2_u18);
                guanyu.setBackgroundResource(R.drawable.segmentedbtn4_u16);
                break;
            case R.id.wancheng:// 任务完成
//                for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
//                    Log.d("smffwmanactivity", "" + fm.getBackStackEntryCount());
//                    fm.popBackStack();
//                }
                app.back = "back";
                transaction.replace(R.id.main_frame,
                        rwccfragment = new RwwcFragment()).commit();
                renwu.setBackgroundResource(R.drawable.segmentedbtn1_u14_selected);
//                wancheng.setBackgroundResource(R.drawable.segmentedbtn2_u18_selected);
                guanyu.setBackgroundResource(R.drawable.segmentedbtn4_u16);
                break;
            case R.id.guanyu:// 关于
                for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
                    Log.d("smffwmanactivity", "" + fm.getBackStackEntryCount());
                    fm.popBackStack();
                }
                app.back = "back";

                transaction.replace(R.id.main_frame, gyfragment = new GyFragment())
                        .commit();
                renwu.setBackgroundResource(R.drawable.segmentedbtn4_u16);
//                wancheng.setBackgroundResource(R.drawable.segmentedbtn2_u18);
                guanyu.setBackgroundResource(R.drawable.segmentedbtn1_u14_selected);
                break;
            case R.id.back:// 返回  getBackStackEntryCount    得到Fragment的管理器，从管理器中得到Fragment当前已加入Fragment回退栈中的fragment的数量
                Log.d("smffwmanactivity", "默认");
                this.finish();
                break;
            case R.id.parent:// 返回 去掉输入法
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(arg0.getWindowToken(), 0);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    public void onloadCount(int count) {
        zhuangxiangNum.setText("" + count);
    }

    /**
     * 接收我的任务信息传过来的任务条数
     */
    public void tranmsg(String num) {
        System.out.println("num:" + num);
        renwuNum.setText(num);
    }

    /**
     * 接收我的任务信息传过来的车牌号
     */
    public void trancid(String cardId) {
        app.cardId = cardId;
    }

	/*
	 * public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode ==
	 * KeyEvent.KEYCODE_BACK) { if("back".equals(app.back)){ Skip.skip(this,
	 * LoginActivity.class, null, 0); } } return super.onKeyDown(keyCode,
	 * event); }
	 */

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

    }


    @Override
    public void onArticleSelected(int position, Bundle bundle, Fragment fragment) {

        switch (position) {
            case StartFwFragment.START_FUWU_TAG:
                StartFwFragment mStartFwFragment = new StartFwFragment();
                mStartFwFragment.setArguments(bundle);
                fm.beginTransaction().remove(fragment)
                        .replace(R.id.main_frame, mStartFwFragment)
                        .addToBackStack(null).commit();
                break;
            case KssmFragment.KSSM_TAG:
                KssmFragment mkFragment = new KssmFragment();
                mkFragment.setArguments(bundle);
                fm.beginTransaction().remove(fragment)
                        .replace(R.id.main_frame, mkFragment).addToBackStack(null)
                        .commit();
                break;
            case MyRenwuFragment.RENWU_TAG:
                for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
                    fm.popBackStack();
                }
                MyRenwuFragment myRenwuFragment = new MyRenwuFragment();
                myRenwuFragment.setArguments(bundle);
                fm.beginTransaction().remove(fragment)
                        .replace(R.id.main_frame, myRenwuFragment).commit();
                break;
            case RwwcFragment.TAG:
                for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
                    fm.popBackStack();
                }
                RwwcFragment rwwcFragment = new RwwcFragment();
                rwwcFragment.setArguments(bundle);
                fm.beginTransaction().remove(fragment)
                        .replace(R.id.main_frame, rwwcFragment).commit();
                break;
            default:
                break;
        }
    }

}
